package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.pojo.Manager;
import com.chengwang.bookstory.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("type")
public class TypeController {
    @Autowired
    TypeService typeService;
    @RequestMapping("/addType")
    public ModelAndView addType(HttpServletRequest request){
        String type = request.getParameter("type");
        System.out.println(type);
        typeService.addType(type);
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        ModelAndView mv=new ModelAndView();
        mv.addObject("manager",manager);
        mv.addObject("mes","添加成功");
        mv.setViewName("manager.html");
        return mv;
    }
//查询所有分类
    @RequestMapping("/queryAllType")
    @ResponseBody
    public List queryAllType(){
        List<String> typeList=typeService.queryAllType();
        return typeList;
    }
    //删除分类
    @RequestMapping("/deleteType")
    @ResponseBody
    public void deleteType(String type){
        typeService.deleteType(type);
    }
    //修改分类
    @RequestMapping("/updateType")
    public ModelAndView updateType(String type1,String type2,HttpServletRequest request){
        typeService.updateType(type1,type2);
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        ModelAndView mv=new ModelAndView();
        mv.addObject("manager",manager);
        mv.setViewName("manager.html");
        return mv;

    }
}
